﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xplang.run;
using xplang.abstractions;
using System.Diagnostics;

namespace xplang.expression
{
  [DebuggerDisplay("{variable}={expr}")]
  public class Attrib : Statement
  {

    public Attrib(ValueReference varRef, Expression expr, bool byRef)
      : base(varRef.resultType)
    {
      this.varRef = varRef;
      this.expr = expr;
      this.byRef = byRef;
    }

    public readonly ValueReference varRef;
    public readonly Expression expr;
    public readonly bool byRef;

    public override Value doIt(Value[] heap, ref FlowState state)
    {
      if (byRef)
        heap[0] = heap[1];
      else
        heap[0] = heap[1].clone();
      return heap[0];
    }

    public override bool isBlock()
    {
      return false;
    }
    //public override void save(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.save(scope,target);
    //    }
    //    target.writeln(";");
    //}

    //public override void toCSharp(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.toCSharp(scope, target);
    //    }
    //    target.writeln(";");
    //}

    //public override void toJavaScript(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.toCSharp(scope, target);
    //    }
    //    target.writeln(";");
    //}
  }
}
